package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Department;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * implementation of GenericDAOInterface
 * @author Enrico Baroni
 *
 */
public class DepartmentDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	

	/**
	 * default constructor
	 */
	public DepartmentDAO () {
		
	}
	
	
	
	/* 
	 * selects one department by Id
	 * @param id
	 * @return department
	 */
	 @Transactional(readOnly = true)
	public Department selectById(int id) {
		 Department department = (Department) getSession().get(Department.class, id);
	    return department;
	}

	/*
	 * retrieves all departments from db
	 * @return List of departments
	 */
	 @Transactional(readOnly = true)
	public List<Department> selectAll() {
	 
	    List<Department> departments = getSession().createQuery("from department").list();
	    return departments;
	}

	/*
	 * inserts a new department in database
	 * retrieves generated id and sets to department instance
	 * @param new department
	 */
	 @Transactional
	public void insert(Department department) {
	    Integer id = (Integer) getSession().save(department);
	    department.setId(id);           
	}

	/*
	 * updates department
	 * @param department to update
	 */
	 @Transactional
	public void update(Department department) {
		getSession().merge(department);
	}

	/*
	 * delete given department
	 * @param department to delete
	 */
	 @Transactional
	public void delete(Department department) {
		getSession().delete(department);
	}



	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void insert(Object newInsert) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void update(Object future) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}
}
